package com.vchello.controller.file;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.plugins.Page;
import com.vchello.bean.file.File;
import com.vchello.bean.file.Folder;
import com.vchello.bean.user.User;
import com.vchello.contants.CONTANTS;
import com.vchello.controller.BaseController;
import com.vchello.service.file.FileService;
import com.vchello.service.file.FolderService;
import com.vchello.util.DownXls;
import com.vchello.util.ToolUtil;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/file")
public class FileController extends BaseController {

	@Autowired
	private FileService fileService;
	@Autowired
	private FolderService folderService;

	private static final String REDIRECT_LOGIN = "redirect:/login";

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {

		User user = getUser();
		if (null == user) {
			return REDIRECT_LOGIN;
		}
		return "file/fileList";

	}

	@RequestMapping(value = "/detail/{fid}", method = RequestMethod.GET)
	public String detail(Model model, Page<File> page, @PathVariable int fid) {
		User user = getUser();
		if (null == user) {
			return REDIRECT_LOGIN;
		}
		if (null == page) {
			page = new Page<File>();
		}
		Folder folder = folderService.findFolderById(fid);
		if(folder.getUserId() != user.getId()) {
			return "404";
		}
		page= fileService.findFileByPage(page, fid);
		model.addAttribute("page", page).addAttribute("fid", fid).addAttribute("folder", folder);
		return "file/fileDetail";

	}

	@RequestMapping(value = "/detail/query", method = RequestMethod.POST)
	public String detail(Model model, File file, Page<File> page, int fid) {
		page = fileService.findFileByPage(page, file);
		Folder folder = folderService.findFolderById(fid);
		model.addAttribute("page", page).addAttribute("folder", folder);
		return "file/query/fileQuery";

	}

	public void updateFile() {

	}

	@RequestMapping(value = "/moveAllFile", method = RequestMethod.POST)
	public void moveAllFile(String fileStr) {
		try {
			User user = getUser();
			if (null == user) {
				error("用户未登录");
				return;
			}
			if (ToolUtil.isEmpty(fileStr)) {
				error("请选择要删除的文件");
				return;
			}
			String[] fileArray = fileStr.split(",");
			File file = null;
			for (String files : fileArray) {
				file = fileService.findFileById(Integer.parseInt(files));
				fileService.deleteFile(file);
			}
			success("删除文件成功");
		} catch (Exception e) {
			error("删除失败");
		}
	}

	@RequestMapping(value = "/moveFile", method = RequestMethod.POST)
	public void moveFile(int id) {
		File file = fileService.findFileById(id);
		if (null == file) {
			error("找不到该文件");
			return;
		}
		boolean isTrue = fileService.deleteFile(file);
		if (isTrue) {
			success("删除文件成功");
			return;
		}
		error("删除文件失败");
	}

	/**
	 * 文件下载
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/downFile/{id}")
	public void downFile(@PathVariable int id) {
		try {
			File fileEntity = fileService.findFileById(id);
			java.io.File file = new java.io.File(CONTANTS.FILE_ROOTPATH + "/" + fileEntity.getUrl());
			if (file.exists()) {
				DownXls.downloadBp(file, request, response);
				return;
			}
			error("下载失败");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
